/** 多sheet的excel导出
 *new Derive({
        bookType: 'xlsx',excel格式
        filename: '123',excel名字
        tHeader: [[], []],每个sheet的表头，可以为空
        list: [this.list1, this.list2],每个sheet的表格数据
        autoWidth: [true, true],是否excel展开
        sheetName: ['sh1', 'sh2'],每个sheet名字
        filterVal: [每个sheet表格数据对应的字段
          ['orderItemId', 'componentTypeId', 'image_uri'],
          ['orderItemId', 'componentTypeId', 'image_uri'],
        ],
      });

      导出多sheet table表格export_table_to_excel([this.$refs.tabDiv1, this.$refs.root], '日报', ['sheet1', 'sheet2']);
 */

class Derive {
    constructor(arg = {}) {
      this.tHeader = arg.tHeader;
      this.list = arg.list;
      this.autoWidth = arg.autoWidth;
      this.sheetName = arg.sheetName;
      this.filterVal = arg.filterVal;
      this.bookType = arg.bookType;
      this.filename = arg.filename;
      this.export2Excel();
    }
  
    // eslint-disable-next-line class-methods-use-this
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]));
    }
  
    export2Excel() {
      require.ensure([], () => {
        // eslint-disable-next-line camelcase
        const { export_json_to_excel } = require('@/lib/Export2Excel.js');
        const newData = [];
        this.filterVal.forEach((el, i) => {
          const filterVal = this.filterVal[i];
          newData.push({
            multiHeader: [],
            header: this.tHeader[i] || [],
            data: this.formatJson(filterVal, this.list[i]),
            merges: [],
            autoWidth: this.autoWidth[i],
            sheetName: this.sheetName[i],
          });
        });
        export_json_to_excel({
          bookType: this.bookType || 'xlsx',
          filename: this.filename || 'excel',
          sheetFile: newData,
        }); // 导出文件名称
      });
    }
  }
  
  export default Derive;
  